export default {
    namespaced:true,
    state:{
        catrData:[]
    },
    mutations:{
        LOG(state){

        },
        SET_CATR(state,payload){

            let target = state.catrData.find(item => item.id === payload.id)
            if(!target){
                let obj = {
                    title:payload.title,
                    price:payload.price,
                    count:1,
                    id:payload.id
                }
                state.catrData.push(obj)
            }else{
                target.count++
            }

            // state.catrData = payload
            // state.catrData.push(payload)
        },
        DE_CATR(state,payload){
            let target = state.catrData.find(item => item.id === payload.id)
            if(target){
                target.count--
            }
            if (target.count ===0){
                state.catrData = state.catrData.filter(item =>item.id !== target.id)
            }
        }
    },
    actions:{
        ADD_CATR(context,payload){
            console.log('catr action---',payload)
            context.commit('SET_CATR',payload)
            context.commit('product/DE_IY',payload.id,{root:true})

        },
        // RE_CT(state,payload){
        //     context.commit('DE_CATR',payload)
        //     context.commit('product/DE_IY',payload,{root:true})
        // },
        RE_CATR(context,payload){
            context.commit('DE_CATR',payload)
            context.commit('product/IE_IY',payload,{root:true})
        }
    },
    getters:{
        total(state){
            return (state.catrData.reduce((total,item)=>{return total += item.count * item.price},0)).toFixed(2)
        }
    }
} 